def get_link_content(url):
    import requests
    from bs4 import BeautifulSoup
    resp = requests.get(url)
    soup = BeautifulSoup(resp.text, 'html.parser')

    body = soup.select_one('body')
    content = 'Empty Content'
    try:
        content = body.text.replace('\n', '').replace('\u2002', '')
    except Exception as e:
        print('Error: ', e)
    return content

def print_bing_search_result(data):
    for item in data:
        print('\n ---\n title: ', item['title'], '\nabstract: ', item['abstract'], '\nlink: ', item['link'])
        item['content'] = ''.join(get_link_content(item['link']))

def print_bing_result_lite(data):
    idx=1
    for item in data:
        print(f' {idx}: ', item['title'], '  ',item['link'],'\n    ', item['abstract'])
        idx+=1
        
def search_with_bing(query):
    import requests
    from bs4 import BeautifulSoup
    from urllib.parse import quote
    url = f'https://cn.bing.com/search?q={quote(query)}'
    headers = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36'
    }
    resp = requests.get(url, headers=headers)
    soup = BeautifulSoup(resp.text, 'html.parser')

    result_elements = soup.select('#b_results > li')
    data = []

    for parent in result_elements:
        if parent.select_one('h2') is None:
            continue

        data.append({
            'title': parent.select_one('h2').text,
            'abstract': parent.select_one('div.b_caption > p').text.replace('\u2002', ' '),
            'link': parent.select_one('div.b_tpcn > a').get('href'),
            'content': ''
        })

    # print_bing_search_result(data)
    # for item in data:
    #     print('\n ---\n title: ', item['title'], '\nabstract: ', item['abstract'], '\nlink: ', item['link'])
    #data[0]['content'] = ''.join(get_link_content(data[0]['link']))
    #print(data[0]['content'])
    
    return data

    # return [item['content'] for item in data]



#search_with_bing('大语言模型')

'''
import pprint
data = search_with_bing('大语言模型')
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(data)
'''